package com.campus.controller;

import com.campus.common.ResponseResult;
import com.campus.dto.TeacherDto;
import com.campus.model.Teacher;
import com.campus.service.TeacherService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 教师控制器类，用于处理教师相关的HTTP请求
 * @author: hjm
 * @date: 2024/11/22
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    private final TeacherService teacherService;

    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    /**
     * 创建教师
     *
     * @param teacherDto 教师DTO对象
     * @return 创建结果
     */
    @PostMapping("/create")
    public ResponseResult<String> createTeacher(@RequestBody TeacherDto teacherDto) {
        int result = teacherService.createTeacher(teacherDto);
        return result > 0 ? ResponseResult.ok("教师创建成功") : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "教师创建失败");
    }

    /**
     * 根据ID获取教师信息
     *
     * @param id 教师ID
     * @return 教师对象
     */
    @GetMapping("/{id}")
    public ResponseResult<Teacher> getTeacherById(@PathVariable int id) {
        Teacher teacher = teacherService.getTeacherById(id);
        return teacher != null ? ResponseResult.ok(teacher) : ResponseResult.error(HttpStatus.NOT_FOUND, "教师不存在");
    }

    /**
     * 根据筛选条件获取教师列表
     *
     * @param subject 科目
     * @param school  学校
     * @return 教师列表
     */
    @GetMapping("/filter")
    public ResponseResult<List<Teacher>> getTeachersByFilter(@RequestParam(required = false) String subject,
                                                             @RequestParam(required = false) String school) {
        List<Teacher> teachers = teacherService.getTeachersByFilter(subject, school);
        return ResponseResult.ok(teachers);
    }
}